﻿using InnoWeb.CEND.BL.Service;
using InnoWeb.CEND.Core.Model.General;
using InnoWeb.CEND.Data;
using InnoWeb.CEND.Data.Infrastructure;
using InnoWeb.CEND.Web.Framework;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace InnoWeb.CEND.UI.MVC.Areas.Admin.Controllers
{
    public class SliderController : Controller
    {
        private static WebSiteContext _context;
        private static IUnitOfWork _unitOfWork;

        private static GenericRepository<SliderContent> _sliderRepository;
        private static SliderService _sliderService;

        public SliderController()
        {
            _context = new WebSiteContext(ConfigurationManager.ConnectionStrings["CendDBConnection"].ConnectionString);
            _unitOfWork = new UnitOfWork(_context);

            _sliderRepository = new GenericRepository<SliderContent>(_context);

            _sliderService = new SliderService(_unitOfWork, _sliderRepository);
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult Index()
        {
            var itemList = _sliderService.GetAllElements().ToList();

            return View(itemList);
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(SliderContent model)
        {
            try
            {
                var imgList = Request.Files;
                var slider = model;

                for (int i = 0; i < Request.Files.Count; i++)
                {
                    var img = Request.Files[i];

                    string fileName = Guid.NewGuid().ToString().Replace("-", "");
                    string serverPath = Server.MapPath("~/Content/Images");
                    //string imagesPath = serverPath + "Content\\Images\\";
                    string thumbPath = Server.MapPath("~/Content/Images/Thumb/");
                    string fullPath = Server.MapPath("~/Content/Images/Full/");

                    ImageUploader.ImageSave(thumbPath, fullPath, img, fileName);

                    if (i == 0)
                    {
                        slider.ImageUrl1 = fileName + ".jpg";
                    }
                    else if (i == 1)
                    {
                        slider.ImageUrl2 = fileName + ".jpg";
                    }
                    else if (i == 1)
                    {
                        slider.ImageUrl3 = fileName + ".jpg";
                    }
                    else if (i == 1)
                    {
                        slider.ImageUrl4 = fileName + ".jpg";
                    }
                    else if (i == 1)
                    {
                        slider.ImageUrl5 = fileName + ".jpg";
                    }
                    else
                    {
                    }

                }
                //var img = Request.Files["fileUpload"];

                //string fileName = Guid.NewGuid().ToString();
                //string serverPath = Server.MapPath("~");
                //string imagesPath = serverPath + "Content\\Images\\";
                //string thumbPath = imagesPath + "Thumb\\";
                //string fullPath = imagesPath + "Full\\";

                //ImageUploader.ImageSave(thumbPath, fullPath, img);

                slider.MottoImage1 = model.MottoImage1;
                slider.MottoImage2 = model.MottoImage2;
                slider.MottoImage3 = model.MottoImage3;
                slider.MottoImage4 = model.MottoImage4;
                slider.MottoImage5 = model.MottoImage5;

                slider.MottoImage1TR = model.MottoImage1TR;
                slider.MottoImage2TR = model.MottoImage2TR;
                slider.MottoImage3TR = model.MottoImage3TR;
                slider.MottoImage4TR = model.MottoImage4TR;
                slider.MottoImage5TR = model.MottoImage5TR;


                slider.ModifiedDate = DateTime.Today;
                slider.CreatedDate = DateTime.Today;
                slider.IsActive = true;
                slider.IsDeleted = false;
                _sliderService.CreateElement(slider);
                _sliderService.SaveChanges();

                Response.Write("<script type='text/javascript'>confirm(\"Data successfully added to database.\"); document.location = 'Index';</script>");

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult Edit(int id)
        {
            var obj = _sliderService.GetElementById(id);
            //return View(new ExpertInfoModel { ExpertInfo = obj });
            return View(obj);
        }

        [HttpPost]
        public ActionResult Edit(int id, SliderContent model)
        {
            try
            {
                var imgList = Request.Files;
                var slider = model;

                for (int i = 0; i < Request.Files.Count; i++)
                {
                    var img = Request.Files[i];

                    string fileName = Guid.NewGuid().ToString().Replace("-", "");
                    string serverPath = Server.MapPath("~/Content/Images");
                    //string imagesPath = serverPath + "Content\\Images\\";
                    string thumbPath = "";
                    string fullPath = Server.MapPath("~/Content/Images/Full/");

                    ImageUploader.ImageSave(thumbPath, fullPath, img, fileName, IsSlider: true);

                    if (i == 0)
                    {
                        slider.ImageUrl1 = fileName + ".jpg";
                    }
                    else if (i == 1)
                    {
                        slider.ImageUrl2 = fileName + ".jpg";
                    }
                    else if (i == 2)
                    {
                        slider.ImageUrl3 = fileName + ".jpg";
                    }
                    else if (i == 3)
                    {
                        slider.ImageUrl4 = fileName + ".jpg";
                    }
                    else if (i == 4)
                    {
                        slider.ImageUrl5 = fileName + ".jpg";
                    }
                    else
                    {
                    }

                }
                //var img = Request.Files["fileUpload"];

                //string fileName = Guid.NewGuid().ToString();
                //string serverPath = Server.MapPath("~");
                //string imagesPath = serverPath + "Content\\Images\\";
                //string thumbPath = imagesPath + "Thumb\\";
                //string fullPath = imagesPath + "Full\\";

                //ImageUploader.ImageSave(thumbPath, fullPath, img);



                slider.ModifiedDate = DateTime.Today;
                //slider.CreatedDate = DateTime.Today;
                //slider.IsActive = true;
                //slider.IsDeleted = false;
                _sliderService.UpdateElement(slider);
                _sliderService.SaveChanges();

                Response.Write("<script type='text/javascript'>confirm(\"Data successfully updated.\"); document.location = 'Index';</script>");

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult Delete(int id)
        {
            var obj = _sliderService.GetElementById(id);
            return View(obj);
        }

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var obj = _sliderService.GetElementById(id);
                obj.IsDeleted = true;
                _sliderService.UpdateElement(obj);
                _sliderService.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

    }
}
